﻿using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WCS.Entity;
using WCS.Helpers;
using WCS.Utils;

namespace WCS.Controls
{
    public partial class WcsTaskAGVView : UIPage
    {
        public WcsTaskAGVView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SortingTask_Load(object sender, EventArgs e)
        {
            TaskTableBind();
            //MawbNoBind();
            this.uiPagination1.ActivePage = 1;
        }

        /// <summary>
        /// 数据库对象
        /// </summary>
        static SqlSugarDapper _SqlSugarDapper = SqlSugarDapper.GetDbContext();

        /// <summary>
        /// 主单绑定
        /// </summary>
        private void MawbNoBind()
        {
            //var id = _SqlSugarDapper.DB.Queryable<WcsTaskAGV>().ToList().GroupBy(g => g.AGVTaskID).ToList();
            //TaskState.Items.Clear();
            //Dictionary<string, string> TaskStateEnum = new Dictionary<string, string>();

            //foreach (var item in id)
            //{
            //    TaskState.Items.Add(item.Key);
            //    TaskStateEnum.Add(item.Key.ToString(), item.Key.ToString());
            //}

            //TaskState.DataSource = new BindingSource(TaskStateEnum, null);
            //TaskState.DisplayMember = "key";
            //TaskState.ValueMember = "value";
        }

        /// <summary>
        /// 成绩绑定
        /// </summary>
        private void TaskTableBind()
        {
            //获取当前页面的index
            int index = this.uiPagination1.ActivePage - 1;
            int pageIndex = this.uiPagination1.ActivePage;
            int size = this.uiPagination1.PageSize;
            int begin = (index * size) + 1;
            int end = (index + 1) * size;
            int count = 0;
            //int cMawbNo = (int)this.TaskState.SelectedIndex;
            //this.dgvScoreList.DataSource = "select seqId,createTime,importNo,uldNo,flightNo,etd,mawbNo,pieces,scanPieces,sortedPieces,lightPieces,stackPieces,totalPieces,receiveTime from UldInfo".QueryStrCount(out count);

            this.dgvScoreList.DataSource = _SqlSugarDapper.DB.Queryable<WcsTaskAGV>()
                .WhereIF(TaskCode.Text != null, w => w.Task_Code.Contains(TaskCode.Text))
                .WhereIF(TrayBarCode.Text != null, w => w.TrayBar_Code.Contains(TrayBarCode.Text))
                .WhereIF(StareLoc.Text != null, w => w.Start_Station.Contains(StareLoc.Text))
                .OrderBy(w => w.Sub_TaskCode, OrderByType.Asc)//首先根据优先级排序   优先级越大月优先
                .OrderBy(w => w.CreateTime)//然后根据创建时间排序
                .ToPageList(pageIndex, size, ref count);

            //uiPagination1.TotalCount = count;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFind_Click(object sender, EventArgs e)
        {
            TaskTableBind();
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            TaskTableBind();
        }
        /// <summary>
        /// 页面改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pagingSource"></param>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            TaskTableBind();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScoreAdd_Click(object sender, EventArgs e)
        {

            //FrmScoreEdit frmScoreEdit = new FrmScoreEdit();
            //frmScoreEdit.Text = "添加成绩信息";
            //DialogResult result = frmScoreEdit.ShowDialog();
            //if (result == DialogResult.Yes)
            //{
            //    frmScoreEdit.Dispose();
            //    TaskTableBind();
            //}
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScoreEdit_Click(object sender, EventArgs e)
        {
            //if (this.dgvScoreList.CurrentRow == null || this.dgvScoreList.RowCount == 0)
            //{
            //    ShowWarningDialog("没有可编辑的数据");
            //    return;
            //}

            //Score score = new Score();
            //score.CourseId = (int)this.dgvScoreList.SelectedRows[0].Cells["courseId"].Value;
            //score.StuId = (int)this.dgvScoreList.SelectedRows[0].Cells["stuId"].Value;
            //score.Grade = (decimal)this.dgvScoreList.SelectedRows[0].Cells["Grade"].Value;

            //FrmScoreEdit frmScoreEdit = new FrmScoreEdit();
            //frmScoreEdit.Text = "修改成绩信息";
            //frmScoreEdit.Tag = score;
            //DialogResult result = frmScoreEdit.ShowDialog();
            //if (result == DialogResult.Yes)
            //{
            //    frmScoreEdit.Dispose();
            //    ScoreTableBind();
            //}
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScoreDelete_Click(object sender, EventArgs e)
        {
            //if (this.dgvScoreList.CurrentRow == null || this.dgvScoreList.RowCount == 0)
            //{
            //    ShowWarningDialog("没有可删除的数据");
            //    return;
            //}
            //Score score = new Score();
            //score.CourseId = (int)this.dgvScoreList.SelectedRows[0].Cells["CourseId"].Value;
            //score.StuId = (int)this.dgvScoreList.SelectedRows[0].Cells["StuId"].Value;
            //int result = new ScoreBLL().Delete(score);
            //if (result == 0)
            //{
            //    ShowWarningDialog("删除失败");
            //}
            //else
            //{

            //    ShowSuccessDialog("删除成功");
            //    ScoreTableBind();
            //}
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            //saveFileDialog1.Filter = "Excel文件|*.xlsx;*.xls";
            //saveFileDialog1.FileName = "成绩信息";
            //DialogResult result = saveFileDialog1.ShowDialog();
            //if (result == DialogResult.OK)
            //{
            //    //获取数据源
            //    DataTable table = new ScoreBLL().GetScoreList();
            //    //获取表格的标题
            //    List<string> title = new List<string>();
            //    foreach (DataGridViewColumn t in this.dgvScoreList.Columns)
            //    {
            //        if (t.HeaderText == "学号" || t.HeaderText == "课程号")
            //            continue;
            //        title.Add(t.HeaderText);
            //    }
            //    //获取文件路径
            //    string path = saveFileDialog1.FileName;
            //    //获取文件的名字、
            //    int i = path.LastIndexOf('\\');
            //    string name = path.Substring(i + 1);

            //    int m = NpoiHelper.ExportExcel(table, title, path, name);
            //    if (m == -1)
            //    {
            //        ShowWarningDialog("数据为空");

            //    }
            //    else
            //    {
            //        ShowSuccessDialog("写入成功");
            //    }
            //}
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Click(object sender, EventArgs e)
        {
            ////显示选择面板
            //FrmSelection frmSelection = new FrmSelection();
            //frmSelection.Tag = this.dgvScoreList.Columns;
            //DialogResult dialogResult = frmSelection.ShowDialog();
            //if (DialogResult.OK == dialogResult)
            //{
            //    GridPrinter.StartPrint(this.dgvScoreList, true, "成绩信息");
            //}
            ////恢复原样
            //foreach (DataGridViewColumn item in this.dgvScoreList.Columns)
            //{
            //    item.Visible = true;
            //}

        }

        private void TaskState_DropDown(object sender, EventArgs e)
        {

        }
    }
}
